package com.friendfeed.api;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Media {
    private String title;
    private String player;
    private List<MediaThumbnail> thumbnails;
    private List<MediaContent> content;

    public Media() {
    }

    public Media(JSONObject jsonObject) throws JSONException {
        if (jsonObject.has("title")) {
            title = jsonObject.getString("title");
        }

        if (jsonObject.has("player") && !jsonObject.isNull("player")) {
            player = jsonObject.getString("player");
        }

        // thumbnails
        if (jsonObject.has("thumbnails") && !jsonObject.isNull("thumbnails")) {
            JSONArray thumbnailArray = jsonObject.getJSONArray("thumbnails");
            thumbnails = new ArrayList<MediaThumbnail>(thumbnailArray.length());
            for (int i = 0; i < thumbnailArray.length(); i++) {
                thumbnails.add(new MediaThumbnail(thumbnailArray.getJSONObject(i)));
            }
        }

        // media
        if (jsonObject.has("content") && !jsonObject.isNull("content")) {
            JSONArray contentArray = jsonObject.getJSONArray("content");
            content = new ArrayList<MediaContent>(contentArray.length());
            for (int i = 0; i < contentArray.length(); i++) {
                content.add(new MediaContent(contentArray.getJSONObject(i)));
            }
        }
    }

    public String getTitle() {
        return title;
    }

    public String getPlayer() {
        return player;
    }

    public List<MediaThumbnail> getThumbnails() {
        return thumbnails;
    }

    public List<MediaContent> getContent() {
        return content;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setPlayer(String player) {
        this.player = player;
    }

    public void setThumbnails(List<MediaThumbnail> thumbnails) {
        this.thumbnails = thumbnails;
    }

    public void setContent(List<MediaContent> content) {
        this.content = content;
    }
}
